﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using System.Xml;
using System.Web;
using System.Threading;
using System.Timers;
using CookComputing.XmlRpc;

namespace uchomeDesktop
{
    public partial class mainForm : Form
    {

        public mainForm()
        {
            InitializeComponent();

            // 设置更新时间
            string intervalString = Properties.Settings.Default.interval;
            int interval = Convert.ToInt32(intervalString);
            timer1.Interval = interval * 1000;

           
            try
            {
                // 获取用户信息
                User user = new User();
                userInfo userInfo = user.getCurrentUserInfo();
                string screenName;
                if (userInfo.realName != "")
                {
                    screenName = userInfo.realName + "(" + userInfo.handle + ")";
                }
                else
                {
                    screenName = userInfo.handle;
                }
                nameLabel.Text = screenName;
                avatarPictureBox.ImageLocation = userInfo.avatarMiddle;

                // 获取迷你博客
                miniBlog miniBlog = new miniBlog();
                miniBlogInfo miniBlogInfo = miniBlog.getOne();
                miniBlogLabel.Text = miniBlogInfo.message;

                // 获取通知
                notification notification = new notification();
                notificationInfo a = notification.get();
                
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }
        }

        private void mainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            User user = new User();
            user.logout();
            Application.Exit();
        }

        private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            aboutForm about = new aboutForm();
            about.ShowDialog();
        }

        private void 退出登陆ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            User user = new User();
            user.logout();
            Application.Exit();
        }

        private void miniBlogLabel_Click(object sender, EventArgs e)
        {
            miniBlogLabel.Hide();
            miniBlogTextBox.Show();
            miniBlogButton.Show();
            miniBlogTextBox.Text = miniBlogLabel.Text;
        }

        private void splitContainer1_Panel2_Click(object sender, EventArgs e)
        {
            // todo
            if (miniBlogLabel.Visible == false)
            {
                miniBlogTextBox.Hide();
                miniBlogButton.Hide();
                miniBlogLabel.Show();
                miniBlogLabel.Text = miniBlogTextBox.Text;
            }
        }

        private void miniBlogLabel_Paint(object sender, PaintEventArgs e)
        {
            int maxCharNum = 46;
            if (miniBlogLabel.Text.Length > maxCharNum)
            {
                miniBlogLabel.Text = miniBlogLabel.Text.Substring(0, maxCharNum) + "...";
            }
        }

        private void 个人设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            personalConfigForm personalConfigForm = new personalConfigForm();
            personalConfigForm.ShowDialog();
        }

        private void 重新登陆ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            User user = new User();
            user.logout();
            this.Hide();
            loginForm loginForm = new loginForm();
            loginForm.Show();    
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.ShowInTaskbar = true;
            this.WindowState = FormWindowState.Normal;
        }

        private void mainForm_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.ShowInTaskbar = false;
            }
        }

        private void miniBlogButton_Click(object sender, EventArgs e)
        {
            // 发送迷你博客
            string message = miniBlogTextBox.Text;
            if (message == string.Empty)
            {
                MessageBox.Show("请填写状态文字");
            }
            else
            {
                miniBlog miniBlog = new miniBlog();
                Boolean ret = miniBlog.post(message);
                if (ret)
                {
                    MessageBox.Show("更新状态成功");
                    if (miniBlogLabel.Visible == false)
                    {
                        miniBlogTextBox.Hide();
                        miniBlogButton.Hide();
                        miniBlogLabel.Show();
                        miniBlogLabel.Text = miniBlogTextBox.Text;
                    }
                }
                else
                {
                    MessageBox.Show("更新状态失败");
                }
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            string uchomeUrl = Properties.Settings.Default.uchUrl;
            string uid = Properties.Settings.Default.uid;
            string profileUrl = uchomeUrl.Trim('/') + "/space.php?uid=" + uid;
            System.Diagnostics.Process.Start(profileUrl); 
        }

        private void timer1_Tick(object sender, EventArgs e)
        {   
            /*
            // 获取通知
            string uchApiUrl = Properties.Settings.Default.uchApiUrl;
            string auth = Properties.Settings.Default.auth;
            string uid = Properties.Settings.Default.uid;
            string postDataStr = "params[uId]=" + uid + "&module=Notifications&method=get&auth=" + auth;
            try
            {
                XmlNode responseRoot;
                string resBody = httpRequest.post(uchApiUrl, postDataStr);
                string notifyBody = string.Empty;

                XmlDocument response = new XmlDocument();
                response.LoadXml(resBody.Trim());

                // 未读消息
                responseRoot = response.SelectSingleNode("root/item[@id='result']/item[@id='message']/item[@id='unread']");
                string unReadMessage = responseRoot.FirstChild.InnerText.ToString();
                if (Convert.ToInt32(unReadMessage) > 0)
                {
                    notifyBody += "您有" + unReadMessage + "条未读消息\n";
                }

                // 未读通知
                responseRoot = response.SelectSingleNode("root/item[@id='result']/item[@id='notification']/item[@id='unread']");
                string unReadNotifications = responseRoot.FirstChild.InnerText.ToString();
                if (Convert.ToInt32(unReadNotifications) > 0)
                {
                    notifyBody += "您有" + unReadMessage + "条未读通知\n";
                }

                // 好友请求
                responseRoot = response.SelectSingleNode("root/item[@id='result']/item[@id='friendRequest']/item[@id='uIds']");
                string friendsRequest = responseRoot.ChildNodes.Count.ToString();
                if (Convert.ToInt32(friendsRequest) > 0)
                {
                    notifyBody += "您有" + unReadMessage + "个好友请求\n";
                }

                // 输出气泡提示
                if (notifyBody != string.Empty)
                {
                    this.notifyIcon1.ShowBalloonTip(5000, "您有新的消息", notifyBody, ToolTipIcon.Info);
                    timer1.Enabled = false;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
             */
            
        }

        private void 检查新版本ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://code.google.com/p/uchomedesktop/downloads/list/"); 
        }

        private void feedListBox_DrawItem(object sender, DrawItemEventArgs e)
        {
            e.DrawBackground();
            Brush myBrush = Brushes.Black;  //初始化字体颜色=黑色
            this.feedListBox.ItemHeight = 90; //设置项高，根据具体需要设置值
            //为每个项设置字体颜色
            //如果不需要可以不写此switch

            if (e.Index % 2 == 0)
            {
                //e.ForeColor = Color.Gray;
            }

            switch (e.Index)
            {
                case 0:
                    myBrush = Brushes.Red;
                    break;
                case 1:
                    myBrush = Brushes.Orange;
                    break;
                case 2:
                    myBrush = Brushes.Purple;
                    break;
                case 4:
                    myBrush = Brushes.White;
                    break;
            }
            e.Graphics.DrawString(feedListBox.Items[e.Index].ToString(), e.Font, myBrush, e.Bounds, null);
            //这句好象可以不要，自己试下
            e.DrawFocusRectangle();  
        }

    }
}